﻿/*****************************************************************
*
*	Article:
*		How to register a system-wide hotkey in a winform GUI application
*
*	Authors:
*		Miles Chen (stainboyx@hotmail.com)
*
*	Reference:
*		http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx
*
*	Build and Run:
*		csc /out:simple.exe /t:winexe csharp-register-system-hotkey-winform.cs & simple
*
*****************************************************************/
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Nia.Sample
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

    public class MainForm : Form
    {
        private const int WM_USER = 0x0400;
        private const int WM_HOTKEY_START = WM_USER + 100;
        private const int WM_HOTKEY_QUIT = WM_USER + 101;

        [DllImport("user32")]
        private extern static bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
        [DllImport("user32")]
        private extern static bool UnregisterHotKey(IntPtr hWnd, int id);

        public MainForm()
        {
            this.Text = "C#全局热键示例";
            this.Controls.Add(new Label()
            {
                Left = 20,
                Top = 20,
                Text = "按CTL+F8启动IE\r\n按CTL+F9退出程序",
                AutoSize = true,
            });

            this.Load += new EventHandler(MainForm_Load);
            this.FormClosed += new FormClosedEventHandler(MainForm_FormClosed);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // 注册热键
            const uint MOD_CONTROL = 0x0002;
            const uint VK_F8 = 0x77;
            const uint VK_F9 = 0x78;
            if (!RegisterHotKey(this.Handle, WM_HOTKEY_START, MOD_CONTROL, VK_F8) ||
            !RegisterHotKey(this.Handle, WM_HOTKEY_QUIT, MOD_CONTROL, VK_F9))
                MessageBox.Show("注册热键失败，也许是热键冲突，请检查后重新启动");
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // 解除热键
            UnregisterHotKey(this.Handle, WM_HOTKEY_START);
            UnregisterHotKey(this.Handle, WM_HOTKEY_QUIT);
        }

        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            if (m.Msg == WM_HOTKEY)
            {
                if ((int)m.WParam == WM_HOTKEY_START)
                    Process.Start("http://g.cn");
                else if ((int)m.WParam == WM_HOTKEY_QUIT)
                    this.Close();
            }
            base.WndProc(ref m);
        }
    }
}
